﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.Channel.Api.Entity;
using Pub.Class;

namespace GTS.Channel.Api.Job {
    /// <summary>
    /// 每天00:00点将数据从GTS.ChannelApi数据库志入到GTS.ChannelApiLog数据库
    /// </summary>
    public class ImportChannelIVRDealLog : IPlugin {
        public void Main(params string[] args) {
            try {
                StringBuilder log = new StringBuilder();
                log.AppendLine();
                log.AppendLine("* GTS.Channel.Api.Job.ImportChannelIVRDealLog 开始处理数据：");

                string date = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                string strSql = @"
--备份前一天的数据
insert into [GTS.ChannelApiLog].dbo.ChannelDealData(
	SPCode,LinkID,SMSKey,LongPhone,Mobile,ReceiveQuery,	ResponseStatus,RequestDate,MountCode,
	GameCode,ChargeCode,Price,IsHidden,DeviceCode,MRDate,MODate,Telcoid,CountryCode,Unit,Area,PLMN,SMSContent,IMSI) 
select SPCode,LinkID,SMSKey,LongPhone,Mobile,ReceiveQuery,
	ResponseStatus,RequestDate,MountCode,GameCode,ChargeCode,Price,IsHidden,DeviceCode,MRDate
    ,MODate,Telcoid,CountryCode,Unit,Area,PLMN,SMSContent,IMSI
from ChannelDealData 
where RequestDate>='{0} 00:00:00.000' and RequestDate<='{0} 23:59:59.999';

insert into [GTS.ChannelApiLog].dbo.IVRDealData(
	SPCode,LinkID,SMSKey,LongPhone,Mobile,Price,FeeTime,RequestDate,ResponseStatus,BeginTime,EndTime) 
select SPCode,LinkID,SMSKey,LongPhone,Mobile,Price,FeeTime,RequestDate,ResponseStatus,BeginTime,EndTime
from IVRDealData 
where RequestDate>='{0} 00:00:00.000' and RequestDate<='{0} 23:59:59.999';";
                int len = Data.Pool("ConnString").ExecSql(strSql.FormatWith(date));
                log.AppendLine("* {0}日的{1}条记录已备份成功。".FormatWith(date, len));

                date = DateTime.Now.AddDays(-8).ToString("yyyy-MM-dd");
                strSql = @"
--清理1个星期前的数据
delete from dbo.ChannelDealData where RequestDate<='{0} 23:59:59.999';
delete from dbo.IVRDealData where RequestDate<='{0} 23:59:59.999';";
                len = Data.Pool("ConnString").ExecSql(strSql.FormatWith(date));
                log.AppendLine("* {0}日之前的{1}条记录已清理。".FormatWith(date, len));
                log.Append("* 操作成功！");
                Base.WriteLog(log.ToString());
            } catch (Exception ex) {
                Base.WriteLog(ex);
            }
        }
    }
}

